package com.xxl.javaWork_15;

import java.io.*;

/**
 * @Author: xiaxinlin
 * @Date: 2020/12/22 18:09
 */
public class IOStreamDemo {
    public static void main(String[] args) {
        File source = new File("D:\\img\\icons\\favicon-32x32.png");
        File daddr = new File("D:\\img\\test\\favicon-32x32.png");
        System.out.println(IOStreamDemo.copyImg(source, daddr));

//        File file = new File("D:\\img\\test\\test.txt");
//        try {
//            System.out.println(IOStreamDemo.readFile(file));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//        File fileTo = new File("D:\\img\\test\\test.txt");
//        System.out.println(IOStreamDemo.writeFile("写入数据", fileTo));
    }

    public static String readFile(File file) throws IOException {
        FileInputStream inputStream = new FileInputStream(file);
        BufferedInputStream BfInput = new BufferedInputStream(inputStream);
        String Filestr="";
        byte b[] = new byte[1024];
        int len;
        while ((len=BfInput.read(b))!=-1){
            Filestr=Filestr+new String(b,0,len);
        }
        BfInput.close();
        inputStream.close();
        return Filestr;
    }

    public static boolean writeFile(String str,File file){
        FileOutputStream outputStream;
        BufferedOutputStream BfOutput;
        try {
            outputStream = new FileOutputStream(file);
            BfOutput = new BufferedOutputStream(outputStream);
            byte b[] = str.getBytes();
            BfOutput.write(b);
            BfOutput.close();
            outputStream.close();
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    public static boolean copyImg(File source,File daddr){
        try {
            File file = daddr.getParentFile();
            if (!file.exists()){
                file.mkdirs();
            }
            BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(source));
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(daddr));
            int len;
            byte b[] = new byte[1024];
            while ((len=inputStream.read(b))!=-1){
                outputStream.write(b,0,len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            return false;
        }
        return true;

    }
}
